﻿/*控制图片大小不超过制定范围*/
function AutoImgSize(e,imgWidth,imgHeight)
	{
		if(e.width>imgWidth)
			e.width=imgWidth;
		if(e.height>imgHeight)
			e.height=imgHeight;
	}
function resizeImage( source_image , max_width , max_height){   
    var image = new Image();   
    image.src = source_image.src;   
    if (!max_width || parseInt(max_width) <= 0){   
        max_width = max_width;   
    }   
    if (!max_height || parseInt(max_height) <= 0){   
        max_height = max_height;   
    }   
    //开始检查图片   
    if(image.width > 0 && image.height > 0 ){   
        var image_rate = 1;   
        if( (max_width / image.width) < (max_height / image.height)){   
            image_rate = max_width / image.width ;   
        }else{   
            image_rate = max_height / image.height ;   
        }   
        if ( image_rate <= 1){   
            source_image.width  = image.width * image_rate;   
            source_image.height = image.height * image_rate;   
        }   
    }   
       
}   

/* Create a new XMLHttpRequest object to talk to the Web server */

var xmlHttp = false;
var flag = "no";///判断是否登录的标志
var amuseTitle,artistName,Price,id,e,olde,oldOnclick;
function CreateXMLObject()
{
    try 
{
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} 
catch (e) 
{
    try 
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    catch (e2) 
    {
        xmlHttp = false;
    }
}
//@end @   /*@cc_on @@if (@_jscript_version >= 5)*/
if (!xmlHttp && typeof XMLHttpRequest != "undefined") {
  xmlHttp = new XMLHttpRequest();
}
}
///judge current user is valide
function callServer() 
{
CreateXMLObject();
  var url = "/WebSite/isLogin.aspx";
  // Open a connection to the server
  xmlHttp.open("GET", url, true);
  // Setup a function for the server to run when it's done
  xmlHttp.onreadystatechange = updatePage;
  // Send the request
  xmlHttp.send(null);
}
function updatePage() {
  if (xmlHttp.readyState == 4&& xmlHttp.status == 200) {
    var response = xmlHttp.responseText;
    flag=response;
    if(flag=="yes")
        {
        //window.open("/WebSite/jump.aspx?id="+id,"","");
        isLogin(amuseTitle,artistName,Price,id,e);
        }
    else
    {
       sAlert('','',id); 
       }
  }
}

//下载调用的函数
function sjudge(amuseTitle,artistName,Price,id,linkObj)
{  
    this.amuseTitle=amuseTitle;
    this.artistName=artistName;
    this.Price=Price;
    this.id=id;
    this.e=linkObj;
    
    /*如果新的事件对象不等于上次的事件对象
    这删除该层，创建新层
    else 如果新的事件对象等于上次的事件对象
    判断该层是否存在，存在则不管，否则创建新层*/
    if(olde!=e)
    {
        if(document.getElementById("msgDiv")!=null)
            removeOldObj();
        this.olde=linkObj;
        callServer();  
    }
    else
    {
       if(document.getElementById("msgDiv")==null) 
            callServer();
    }
}
///登录调用的方法=====================================================================
/////================================================================================
///==================================================================================
function sAlert(str,stitle,id){
var msgw,msgh,bordercolor;
msgw=200;//提示窗口的宽度
msgh=150;//提示窗口的高度
titleheight=25 //提示窗口标题高度
bordercolor="#78B664";//提示窗口的边框颜色

var sWidth,sHeight;
sWidth=document.body.offsetWidth;//浏览器工作区域内页面宽度
sHeight=screen.height;//屏幕高度（垂直分辨率）


////背景层（大小与窗口有效区域相同，即当弹出对话框时，背景显示为放射状透明灰色）
//var bgObj=document.createElement("div");//创建一个div对象（背景层）
////定义div属性，即相当于
////<div id="bgDiv" style="position:absolute; top:0; background-color:#777; filter:progid:DXImagesTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75); opacity:0.6; left:0; width:918px; height:768px; z-index:10000;"></div>
//bgObj.setAttribute('id','bgDiv');
//bgObj.style.position="absolute";
//bgObj.style.top="0";
//bgObj.style.background="#777";
//bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=10,finishOpacity=75";
//bgObj.style.opacity="1";
//bgObj.style.left="0";
//bgObj.style.width=sWidth + "px";
//bgObj.style.height=sHeight*2 + "px";
//bgObj.style.zIndex = "10000";
//document.body.appendChild(bgObj);//在body内添加该div对象


var msgObj=document.createElement("div")//创建一个div对象（提示框层）
//定义div属性，即相当于
//<div id="msgDiv" align="center" style="background-color:white; border:1px solid #336699; position:absolute; left:50%; top:50%; font:12px/1.6em Verdana,Geneva,Arial,Helvetica,sans-serif; margin-left:-225px; margin-top:npx; width:400px; height:100px; text-align:center; line-height:25px; z-index:100001;"></div>
msgObj.setAttribute("id","msgDiv");
msgObj.setAttribute("align","center");
msgObj.style.background="white";
msgObj.style.border="1px solid " + bordercolor;
msgObj.style.position = "absolute";
//取得该层x、y的坐标
var x = getObjPosition(e).x;
var y = getObjPosition(e).y;
//alert(x+":"+y);

msgObj.style.left =x+"px"; //"50%";
msgObj.style.top = y+"px";"20%";
msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
//msgObj.style.marginLeft = "-225px" ;
//msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";
msgObj.style.width = msgw + "px";
msgObj.style.height =msgh + "px";
msgObj.style.textAlign = "center";
msgObj.style.lineHeight ="25px";
msgObj.style.zIndex = "10001";
msgObj.style.color="#666666";
msgObj.onclick="";

var title=document.createElement("h4");//创建一个h4对象（提示框标题栏）
//定义h4的属性，即相当于
//< h4 id="msgTitle" align="right" style="margin:0; padding:3px; background-color: #336699; filter:progid:DXImageTransform.Microsoft.Alpha(startX=20, startY =20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100); opacity: 0.75; border:1px solid #336699; height:18px; font:12px Verdana,Geneva,Arial, Helvetica,sans-serif; color:white; cursor:pointer;" onclick="">关闭< /h4>
title.setAttribute("id","msgTitle");
title.setAttribute("align","right");
title.style.margin="0";
title.style.padding="3px";
title.style.background=bordercolor;
title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
title.style.opacity="0.75";
title.style.border="1px solid " + bordercolor;
title.style.height="18px";
title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";
title.style.color="white";
title.innerHTML="<span onclick='removeObj();'  style='cursor:pointer'>关闭</span>";
//title.onclick=removeObj;

var button=document.createElement("input");//创建一个input对象（提示框按钮）
//定义input的属性，即相当于
//<input type="button" align="center" style="width:100px; align:center; margin-left:250px; margin-bottom:10px;" value="关闭">
button.setAttribute("type","button");
button.setAttribute("value","关闭");
button.style.width="60px";
button.style.align="center";
button.style.marginLeft="250px";
button.style.marginBottom="10px";
button.style.background=bordercolor;
button.style.border="1px solid "+ bordercolor;
button.style.color="white";
button.onclick=removeObj;

var btnLogin=document.createElement("input");//创建一个登录input对象（提示框按钮）
//定义input的属性，即相当于
btnLogin.setAttribute("type","button");
btnLogin.setAttribute("value","登录");
btnLogin.style.cursor="hand";
btnLogin.style.width="60px";
btnLogin.style.align="center";
//btnLogin.style.marginLeft="250px";
btnLogin.style.marginBottom="10px";
btnLogin.style.background=bordercolor;
btnLogin.style.border="1px solid "+ bordercolor;
btnLogin.style.color="white";
btnLogin.onclick=login;


var btnReg=document.createElement("input");//创建注册一个input对象（提示框按钮）
//定义input的属性，即相当于
btnReg.setAttribute("type","button");
btnReg.setAttribute("value","注册");
btnReg.style.cursor="hand";
btnReg.style.width="60px";
btnReg.style.align="center";
btnReg.style.marginLeft="10px";
btnReg.style.marginBottom="10px";
btnReg.style.background=bordercolor;
btnReg.style.border="1px solid "+ bordercolor;
btnReg.style.color="white";
btnReg.onclick=reg;

function reg()
{
    removeObj();
    window.open("/WebSite/reg.aspx","","");
}

function login()
{

var strUserName=document.getElementById("username").value;
var strPass=document.getElementById("pass").value;
if(strUserName.length==0)
{
    alert("请输入用户名");
    document.getElementById("username").focus();
    return;
}
if(strPass.length==0)
{
    alert("请输入密码");
    document.getElementById("pass").focus();
    return;
}
removeObj();
//test.UserLogin(strUserName,strPass,loginUser_callback);
//跳转到另外页面登录
var url = "/WebSite/operate.aspx?actionid="+escape(strUserName)+"&actionstr="+escape(strPass)+"&id="+id;
var state = "";
if(e.id=="buy")
    url += "&action=buy";
else
    state="toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no";
//alert(url);
 window.open(url,"",state);

//loginUser_callback("0");
}

e.appendChild(msgObj);
//document.body.appendChild(msgObj);//在body内添加提示框div对象msgObj
document.getElementById("msgDiv").appendChild(title);//在提示框div中添加标题栏对象title

var txt=document.createElement("p");//创建一个p对象（提示框提示信息）
//定义p的属性，即相当于
//<p style="margin:1em 0;" id="msgTxt">测试效果</p>
txt.style.margin="1em 0"
txt.setAttribute("id","msgTxt");
txt.innerHTML="<strong>Coodoo商城注册用户请直接登录</strong></br>用户名:<input id='username' width='170' name='username' /><br>密&nbsp;&nbsp;码:<input id='pass' type='PassWord' width='170'  name='pass' /></br></br>";//来源于函数调用时的参数值
document.getElementById("msgDiv").appendChild(txt);//在提示框div中添加提示信息对象txt
document.getElementById("msgDiv").appendChild(btnLogin);
document.getElementById("msgDiv").appendChild(btnReg);
}


function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值
{
var Days = 30; //此 cookie 将被保存 30 天
var exp   = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数       
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)//删除cookie
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

function logout()
{
    delCookie("userID");
    window.location.reload();
}

function removeObj(){//点击标题栏触发的事件
if(document.getElementById("bgDiv")!=null)
     e.removeChild(document.getElementById("bgDiv"));//删除背景层Div
   // document.body.removeChild(document.getElementById("bgDiv"));//删除背景层Div
//document.getElementById("msgDiv").removeChild(document.getElementById("msgTitle"));//删除提示框的标题栏
if(document.getElementById("msgDiv")!=null)
     e.removeChild(document.getElementById("msgDiv"));//删除提示框层
    //document.body.removeChild(document.getElementById("msgDiv"));//删除提示框层
}
///删除先前的层
function removeOldObj(){//点击标题栏触发的事件
if(document.getElementById("bgDiv")!=null)
     olde.removeChild(document.getElementById("bgDiv"));//删除背景层Div
   // document.body.removeChild(document.getElementById("bgDiv"));//删除背景层Div
//document.getElementById("msgDiv").removeChild(document.getElementById("msgTitle"));//删除提示框的标题栏
if(document.getElementById("msgDiv")!=null)
     olde.removeChild(document.getElementById("msgDiv"));//删除提示框层
    //document.body.removeChild(document.getElementById("msgDiv"));//删除提示框层

}

function juge()
{
    if(Price=="0")
    {
        alert("免费物品，不能购买");
        return false;
    }
    return true;
}

//已经登录的用户=====================================================================
/////================================================================================
///==================================================================================

function isLogin(amuseTitle,artistName,Price,pid,e){
    
    //alert(window.event.srcElement.ClientScreenWidth);
var msgw,msgh,bordercolor;
msgw=200;//提示窗口的宽度
msgh=150;//提示窗口的高度
titleheight=25 //提示窗口标题高度
bordercolor="#78B664";//提示窗口的边框颜色

var sWidth,sHeight;
sWidth=document.body.offsetWidth;//浏览器工作区域内页面宽度
sHeight=screen.height;//屏幕高度（垂直分辨率）


var msgObj=document.createElement("div")//创建一个div对象（提示框层）
//定义div属性，即相当于
//<div id="msgDiv" align="center" style="background-color:white; border:1px solid #336699; position:absolute; left:50%; top:50%; font:12px/1.6em Verdana,Geneva,Arial,Helvetica,sans-serif; margin-left:-225px; margin-top:npx; width:400px; height:100px; text-align:center; line-height:25px; z-index:100001;"></div>
msgObj.setAttribute("id","msgDiv");
msgObj.setAttribute("align","center");
msgObj.style.background="white";
msgObj.style.border="1px solid " + bordercolor;
msgObj.style.position = "absolute";
msgObj.setAttribute("class","msgDivcss");
//计算DIV左上角的位置

var x = getObjPosition(e).x;
var y = getObjPosition(e).y;
//alert(x+":"+y);

msgObj.style.left = x+"px";//"50%";
msgObj.style.top =y+"px"; //"20%";
msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
//msgObj.style.marginLeft = "-225px" ;
//msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";
msgObj.style.width = msgw + "px";
msgObj.style.height =msgh + "px";
msgObj.style.textAlign = "left";
msgObj.style.lineHeight ="25px";
msgObj.style.zIndex = "10001";
msgObj.style.color="#666666";

var title=document.createElement("h4");//创建一个h4对象（提示框标题栏）
//定义h4的属性，即相当于
//< h4 id="msgTitle" align="right" style="margin:0; padding:3px; background-color: #336699; filter:progid:DXImageTransform.Microsoft.Alpha(startX=20, startY =20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100); opacity: 0.75; border:1px solid #336699; height:18px; font:12px Verdana,Geneva,Arial, Helvetica,sans-serif; color:white; cursor:pointer;" onclick="">关闭< /h4>
title.setAttribute("id","msgTitle");
title.setAttribute("align","right");
title.style.margin="0";
title.style.padding="3px";
title.style.background=bordercolor;
title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
title.style.opacity="0.75";
title.style.border="1px solid " + bordercolor;
title.style.height="18px";
title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";
title.style.color="white";
title.innerHTML="请选择&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span onclick='removeObj()' style='cursor:pointer'>关闭</span>";
//title.onclick=removeObj;

//document.body.appendChild(msgObj);//在body内添加提示框div对象msgObj
e.appendChild(msgObj);
document.getElementById("msgDiv").appendChild(title);//在提示框div中添加标题栏对象title

var txt=document.createElement("p");//创建一个p对象（提示框提示信息）
//定义p的属性，即相当于
//<p style="margin:1em 0;" id="msgTxt">测试效果</p>
txt.style.marginLeft="10px";
txt.setAttribute("id","msgTxt");
var urlStr="onclick='removeObj();' Target='_black' href='/WebSite/Products/Car.aspx?id="+pid+"&flag=1'";
if(Price=="0")
{
    urlStr="onclick='alert(\"免费物品，不能购买\");' href='#'";
   }
if(artistName=="3")
    txt.innerHTML="</br>软件名称:"+amuseTitle+"<br>版权公司:酷动数码有限公司</br>价格:"+Price+"<br/><a "+urlStr+">放入购物车</a>&nbsp;&nbsp;&nbsp;<a href='javascript:;' onclick='removeObj();lineDown();' >直接下载</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:;' onclick='removeObj()'>取消</a>";
else if(artistName=="4")
    txt.innerHTML="</br>培训资料:"+amuseTitle+"<br>版权公司:酷动数码有限公司</br>价格:"+Price+"<br/><a "+urlStr+">放入购物车</a>&nbsp;&nbsp;&nbsp;<a href='javascript:;' onclick='removeObj();lineDown();' >直接下载</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:;' onclick='removeObj()'>取消</a>";
else
    txt.innerHTML="</br>歌曲名称:"+amuseTitle+"<br>歌&nbsp;&nbsp;&nbsp;手:"+artistName+"</br>价格:"+Price+"<br/><a "+urlStr+">放入购物车</a>&nbsp;&nbsp;&nbsp;<a href='javascript:;' onclick='removeObj();lineDown();' >直接下载</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:;' onclick='removeObj()'>取消</a>";//来源于函数调用时的参数值

document.getElementById("msgDiv").appendChild(txt);//在提示框div中添加提示信息对象txt
}


//获取元素的纵坐标、横坐标
function   getObjPosition(e)   
  {   
        //alert(e);
      var   p   =   new   Object();   
      p.x   =   e.offsetLeft;//+e.offsetHeight;    
      p.y   =   e.offsetTop+e.offsetHeight;   
      //alert(e.offsetTop);
     // alert(p.x+":"+p.y);
      while   (e.offsetParent)   
      {   
          e   =   e.offsetParent
          p.x   +=   e.offsetLeft;   
          p.y   +=   e.offsetTop;  
          //alert(p.x+":"+p.y);
      }   
      return   p;  
      alert(p.x+":"+p.y);
  }   
function lineDown()
{
    var url = "/WebSite/jump.aspx?id="+id;
    window.open(url,"","toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
}

function infocus(e)
{
    e.focus();
}